package com.dxl.web.account.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.consts.SysConsts;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.entity.account.Account;
import com.dxl.service.account.AccountService;
import com.dxl.web.common.controller.BaseController;

/**
 * 账号信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/account")
@RequirePermission(value={"ACCOUNT-MODULE"}, name="账户模块", desc="包括账户增、删、改、查及授权")
public class AccountController extends BaseController {
	@Autowired
	private AccountService accountService;
	
	@RequestMapping("")
	@RequirePermission(value={"ACCOUNT-VIEW"}, name="账户查看", desc="查看账户信息")
	public ModelAndView account() {
		ModelAndView mv = new ModelAndView("account/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"ACCOUNT-VIEW"}, name="账户查看", desc="查看账户信息")
	public Page<Account> list(String keyword, Integer state, Integer page, Integer rows) {
		Page<Account> p = this.accountService.list(keyword, state, page, rows);
		return p;
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"ACCOUNT-VIEW"}, name="账户查看", desc="查看账户信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("account/edit");
		if (Utils.isNotBlank(id)) {
			Account account = this.baseService.get(Account.class, id);
			if (Utils.isNotNull(account)) {
				account.setPassword(SysConsts.SIX_STAR);
			}
			mv.addObject("entity", account);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"ACCOUNT-MODIFY"}, name="账户修改", desc="修改账户信息")
	public RespData save(Account account) {
		return accountService.save(account);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"ACCOUNT-DEL"}, name="账号删除", desc="删除账号信息")
	public RespData del(String[] ids) {
		return accountService.del(ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"ACCOUNT-STATE"}, name="账号状态修改", desc="修改账号状态")
	public RespData state(String[] ids, Integer state) {
		return this.state(Account.class, ids, state);
	}

	@ResponseBody
	@RequestMapping(value = "/authorize", method = RequestMethod.GET)
	@RequirePermission(value={"ACCOUNT-AUTHORIZE"}, name="账号授权", desc="账号授权")
	public ModelAndView authorize(String accountId) {
		ModelAndView mv = new ModelAndView("account/role/selection");
		if (Utils.isNotBlank(accountId)) {
			mv.addObject("account", this.baseService.get(Account.class, accountId));
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = "/authorize", method = RequestMethod.POST)
	@RequirePermission(value={"ACCOUNT-AUTHORIZE"}, name="账号授权", desc="账号授权")
	public RespData authorize(String accountId, String[] roleIds) {
		this.accountService.authorize(accountId, roleIds);
		return RespData.instance(RespData.SUCCESS);
	}
}
